import os
import requests
import pandas as pd
from tqdm import tqdm
from loguru import logger
from concurrent.futures import ThreadPoolExecutor

# from config import workers

def get_p_data(page):
    url = f"https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=30&isVerify=1&pageNo={page}"
    try:
        r = requests.get(url=url)
        r.raise_for_status()
        r = r.json()
        # 获取数据
        r_list = r['value']['list']
        # 数据存放
        results = []
        for data in r_list:
            # 期号
            lotteryDrawNum = data["lotteryDrawNum"]
            # 开奖日期
            lotteryDrawTime = data["lotteryDrawTime"]
            # 开奖设备
            lotteryEquipmentCount = data["lotteryEquipmentCount"]
            # 抽取流动资金
            drawFlowFund = data["drawFlowFund"]
            # 开奖结果
            lotteryDrawResult = data["lotteryDrawResult"]
            # 兑付时间
            lotteryPaidBeginTime = data["lotteryPaidBeginTime"]
            lotteryPaidEndTime = data["lotteryPaidEndTime"]
            # 开奖pdf
            drawPdfUrl = data["drawPdfUrl"]
            # 奖池余额
            poolBalanceAfterdraw = data["poolBalanceAfterdraw"]
            # 销售额
            totalSaleAmount = data["totalSaleAmount"]
            # 一等奖基本数及组及追加数
            stakeCount_1 = data["prizeLevelList"][0]["stakeCount"]
            stakeCount_1_add = data["prizeLevelList"][1]["stakeCount"]
            # 二等奖基本数及组及追加数
            stakeCount_2 = data["prizeLevelList"][2]["stakeCount"]
            stakeCount_2_add = data["prizeLevelList"][3]["stakeCount"]
            # 保存每期数据
            results.append([lotteryDrawNum, lotteryDrawTime, lotteryEquipmentCount, drawFlowFund, lotteryDrawResult, lotteryPaidBeginTime, lotteryPaidEndTime, drawPdfUrl, poolBalanceAfterdraw, totalSaleAmount, stakeCount_1, stakeCount_1_add, stakeCount_2, stakeCount_2_add])
        
        return results

    except Exception as e:
        logger.error(f"请求第{page}页数据出错：{str(e)}")
        return []
    
def get_lottery_data():
    logger.info("--"*15 + "爬虫程序启动" + "--"*15)

    # 检查数据存储路径是否存在
    if not os.path.exists("data_store/dlt_data"):
        os.makedirs("data_store/dlt_data")

    try:
        # 获取总页数
        r = requests.get("https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=30&isVerify=1&pageNo=1")
        r.raise_for_status()
        r = r.json()
        pages = r['value']['pages']

        # 创建线程池
        with ThreadPoolExecutor(max_workers=2) as executor:
            # 提交任务
            tasks = [executor.submit(get_p_data, page) for page in range(1, int(pages) + 1)]
            results = []

            # 获取结果
            for future in tqdm(tasks, desc="正在爬取数据", total=len(tasks)):
                results += future.result()
                
        results = pd.DataFrame(results, columns=["期数", "开奖时间", "开奖设备", "抽取流动资金", "开奖结果", "兑奖开始时间", "兑奖截止时间", "pdf链接", "奖池余额", "销售总额", "一等奖注数", "一等奖追加注数", "二等奖注数", "二等奖追加注数"])
        results.to_csv("data_store/dlt_data/dlt_all_data.csv", index=False)

        logger.info("信息爬取完毕，保存完成！")
    except Exception as e:
        logger.error(f"爬取数据出错：{str(e)}")
    finally:
        logger.info("--"*15 + "爬虫程序结束" + "--"*15)

if __name__ == "__main__":
    get_lottery_data()
